<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "content".
 *
 * @property string $id
 * @property string $title
 * @property string $thumb
 * @property string $detail
 * @property string $create_time
 * @property string $pub_time
 * @property string $copyfrom
 * @property string $manager_id
 * @property string $summary
 */
class Content extends \yii\db\ActiveRecord
{
    public $rel_time;
    public $channel_name;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'content';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['detail', 'summary'], 'string'],
            [['create_time', 'pub_time', 'manager_id'], 'integer'],
            [['title'], 'string', 'max' => 512],
            [['title'], 'required', 'message' => '标题不能为空'],
            [['thumb', 'copyfrom'], 'string', 'max' => 128]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'thumb' => '缩略图',
            'detail' => '详细描述',
            'rel_time'=>'发布时间',
            'channel_name'=>'频道',
            'create_time' => '创建时间',
            'pub_time' => '发布时间',
            'copyfrom' => '来源',
            'manager_id' => '后台操作管理员',
            'summary' => '摘要',
        ];
    }
    public function save($runValidation = true, $attributeNames = null){
        if($this->isNewRecord){
            $this->create_time=time();
            if($this->rel_time ==NULL){
                $this->pub_time=time();
            }else{
                $this->pub_time=strtotime($this->rel_time);
            }
            $this->manager_id=yii::$app->session->get('uid');
        }else{
            if($this->rel_time ==NULL){
                $this->pub_time=time();
            }else{
                $this->pub_time=strtotime($this->rel_time);
            }
        }
        return parent::save($runValidation, $attributeNames);
    }
    public function getChannel_id()
    {
        if($this->isNewRecord) return [];
        $joins = Cncjoin::find()->where(['content_id'=>$this->id])->asArray()->indexBy('channel_id')->all();
        return array_keys($joins);
    }
    public static $reStr = [];
    public function getChannel($fid=0,$num=0){
        //找符合$fid的分类
        $arr=Channel::find()->where(['fid'=>$fid])->asArray()->all();
        //自己调用自己
        $spaceStr=str_repeat("--", $num*2);
        foreach($arr as $v){
            self::$reStr[$v['cid']]=$spaceStr.$v['cname'];
            $this->getChannel($v['cid'],++$num);
            $num--;
        }
        return self::$reStr;
    }
    public static function getChannel_str($fid=0,$num=0){
        //找符合$fid的分类
        $arr=Channel::find()->where(['fid'=>$fid])->asArray()->all();
        //自己调用自己
        $spaceStr=str_repeat("-", $num*2);
        foreach($arr as $v){
            self::$reStr[$v['cid']]=$spaceStr.$v['cname'];
           self::getChannel_str($v['cid'],++$num);
            $num--;
        }
        return self::$reStr;
    }
}
